package mortal.game.control
{
    import com.gengine.core.frame.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.rules.*;
    import mortal.game.scene.ai.*;
    import mortal.game.scene.player.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class StatusController extends Controller
    {
        private var _guardTimer:SecTimer;

        public function StatusController()
        {
            
        }

        private function startGuardTimer() : void
        {
            if (this._guardTimer == null)
            {
                this._guardTimer = new SecTimer(5);
                this._guardTimer.addListener(TimerType.ENTERFRAME, this.onGuardTimerHandler);
            }
            this._guardTimer.delay = 5;
            if (!this._guardTimer.running)
            {
                this._guardTimer.start();
            }
            
        }

        private function onGuardTimerHandler(param1:SecTimer) : void
        {
            var _loc_2:IEntity = null;
            if (cache.status.currentStatus == StatusType.AutoPath)
            {
                if (!SkillProgress.instance.running && !RolePlayer.instance.isWorking && !RolePlayer.instance.isMountWorking)
                {
                }
            }
            else if (cache.status.currentStatus == StatusType.AutoFight)
            {
                if (!AIManager.isCheckBossTimer() && RolePlayer.instance.isCanControlAttack)
                {
                    if (!RolePlayer.instance.isAttackAction && !RolePlayer.instance.collecting && !SkillProgress.instance.running && !RolePlayer.instance.isWorking && !RolePlayer.instance.isMountWorking)
                    {
                        _loc_2 = GameController.scene.selectEntity;
                        if (_loc_2 != null)
                        {
                            AIManager.onAIControl(_loc_2, AIType.AI_AutoFight);
                        }
                        else
                        {
                            AIManager.onAIControl(null, AIType.AI_AutoFight);
                        }
                    }
                }
            }
            else
            {
                this.stopGuardTimer();
            }
            
        }

        private function stopGuardTimer() : void
        {
            if (this._guardTimer && this._guardTimer.running)
            {
                this._guardTimer.stop();
                this._guardTimer.isDelete = false;
            }
            
        }

        private function onAutoFightBeginHandler(event:DataEvent) : void
        {
            if (AutoFightRule.autoFightPassMap == true)
            {
                this.onAutoPathBeginHandler(null);
                
            }
            if (cache.status.currentStatus == StatusType.AutoFight)
            {
                
            }
            cache.status.currentStatus = StatusType.AutoFight;
            RolePlayer.instance.setRoleStatus(cache.status.currentStatus);
            this.startGuardTimer();
            
        }

        private function onAutoFightEndHandler(event:DataEvent) : void
        {
            AutoFightRule.flag = 0;
            if (cache.status.currentStatus == StatusType.AutoFight)
            {
                cache.status.currentStatus = 0;
                this.hideStatus();
            }
            
        }

        private function onAutoPathBeginHandler(event:DataEvent) : void
        {
            if (cache.status.currentStatus == StatusType.AutoPath)
            {
                
            }
            if (!RolePlayer.instance.hasMount)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.KeyControllMounts));
            }
            cache.status.currentStatus = StatusType.AutoPath;
            RolePlayer.instance.setRoleStatus(cache.status.currentStatus);
            this.startGuardTimer();
            
        }

        private function onAutoPathEndHandler(event:DataEvent) : void
        {
            if (cache.status.currentStatus == StatusType.AutoPath)
            {
                cache.status.currentStatus = 0;
                this.hideStatus();
            }
            
        }

        public function hideStatus() : void
        {
            RolePlayer.instance.setRoleStatus(cache.status.currentStatus);
            
        }

        public function stageResize() : void
        {
            
        }

        override protected function initServer() : void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.BeginToFight, this.onAutoFightBeginHandler);
            Dispatcher.addEventListener(EventName.BeginToFightShortcut, this.onAutoFightBeginHandler);
            Dispatcher.addEventListener(EventName.OverFight, this.onAutoFightEndHandler);
            Dispatcher.addEventListener(EventName.AutoPathBegin, this.onAutoPathBeginHandler);
            Dispatcher.addEventListener(EventName.AutoPathEnd, this.onAutoPathEndHandler);
            
        }

    }
}
